package com.dianping.cat.configuration.server.filter.transform;

import com.dianping.cat.configuration.server.filter.IEntity;
import com.dianping.cat.configuration.server.filter.entity.AtomicTreeConfig;
import com.dianping.cat.configuration.server.filter.entity.CrashLogDomain;
import com.dianping.cat.configuration.server.filter.entity.ServerFilterConfig;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;














public class DefaultSaxParser
  extends DefaultHandler
{
/*  37 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  39 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

/*  41 */   private Stack<String> m_tags = new Stack();

/*  43 */   private Stack<Object> m_objs = new Stack();

  private IEntity<?> m_entity;

/*  47 */   private StringBuilder m_text = new StringBuilder();


/*  50 */   public static ServerFilterConfig parse(InputStream in) throws SAXException, IOException { return (ServerFilterConfig)parseEntity(ServerFilterConfig.class, new InputSource((InputStream)removeBOM(in))); }



/*  54 */   public static ServerFilterConfig parse(Reader reader) throws SAXException, IOException { return (ServerFilterConfig)parseEntity(ServerFilterConfig.class, new InputSource((Reader)removeBOM(reader))); }



/*  58 */   public static ServerFilterConfig parse(String xml) throws SAXException, IOException { return (ServerFilterConfig)parseEntity(ServerFilterConfig.class, new InputSource(new StringReader((String)removeBOM(xml)))); }



  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  64 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  65 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  67 */       factory.setValidating(false);
/*  68 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  70 */       factory.newSAXParser().parse(is, handler);
/*  71 */       return (T)handler.getEntity();
/*  72 */     } catch (ParserConfigurationException e) {
/*  73 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }


  public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  82 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }




  private static <T> T removeBOM(T obj) throws IOException {
/*  88 */     if (obj instanceof String) {
/*  89 */       String str = (String)obj;

/*  91 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  92 */         return (T)str.substring(1);
      }
/*  94 */       return obj;
    }
/*  96 */     if (obj instanceof InputStream) {
/*  97 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  99 */       in.mark(3);

/* 101 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/* 102 */         in.reset();
      }

/* 105 */       return (T)in;
/* 106 */     }  if (obj instanceof Reader) {
/* 107 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 109 */       in.mark(1);

/* 111 */       if (in.read() != 65279) {
/* 112 */         in.reset();
      }

/* 115 */       return (T)in;
    }
/* 117 */     return obj;
  }



  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 123 */     if (value == null || value.length() == 0) {
/* 124 */       return defaultValue;
    }

/* 127 */     if (type == Boolean.class)
/* 128 */       return (T)Boolean.valueOf(value); 
/* 129 */     if (type == Integer.class)
/* 130 */       return (T)Integer.valueOf(value); 
/* 131 */     if (type == Long.class)
/* 132 */       return (T)Long.valueOf(value); 
/* 133 */     if (type == Short.class)
/* 134 */       return (T)Short.valueOf(value); 
/* 135 */     if (type == Float.class)
/* 136 */       return (T)Float.valueOf(value); 
/* 137 */     if (type == Double.class)
/* 138 */       return (T)Double.valueOf(value); 
/* 139 */     if (type == Byte.class)
/* 140 */       return (T)Byte.valueOf(value); 
/* 141 */     if (type == Character.class) {
/* 142 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 144 */     return (T)value;
  }




/* 150 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 155 */   public void endDocument() { this.m_linker.finish(); }



  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 160 */     if (uri == null || uri.length() == 0) {
/* 161 */       Object currentObj = this.m_objs.pop();
/* 162 */       String currentTag = (String)this.m_tags.pop();

/* 164 */       if (currentObj instanceof ServerFilterConfig) {
/* 165 */         ServerFilterConfig serverFilterConfig = (ServerFilterConfig)currentObj;

/* 167 */         if ("transaction-type".equals(currentTag)) {
/* 168 */           serverFilterConfig.addTransactionType(getText());
/* 169 */         } else if ("transaction-name".equals(currentTag)) {
/* 170 */           serverFilterConfig.addTransactionName(getText());
/* 171 */         } else if ("domain".equals(currentTag)) {
/* 172 */           serverFilterConfig.addDomain(getText());
        }
      }
    }

/* 177 */     this.m_text.setLength(0);
  }


/* 181 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 185 */   protected String getText() { return this.m_text.toString(); }


  private void parseForAtomicTreeConfig(AtomicTreeConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 189 */     this.m_objs.push(parentObj);
/* 190 */     this.m_tags.push(qName);
  }

  private void parseForCrashLogDomain(CrashLogDomain parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 194 */     this.m_objs.push(parentObj);
/* 195 */     this.m_tags.push(qName);
  }

  private void parseForServerFilterConfig(ServerFilterConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 199 */     if ("transaction-types".equals(qName) || "transaction-type".equals(qName) || "transaction-names".equals(qName) || "transaction-name".equals(qName) || "domains".equals(qName) || "domain".equals(qName)) {
/* 200 */       this.m_objs.push(parentObj);
/* 201 */     } else if ("crash-log-domain".equals(qName)) {
/* 202 */       CrashLogDomain crashLogDomain = this.m_maker.buildCrashLogDomain(attributes);

/* 204 */       this.m_linker.onCrashLogDomain(parentObj, crashLogDomain);
/* 205 */       this.m_objs.push(crashLogDomain);
/* 206 */     } else if ("atomic-tree-config".equals(qName)) {
/* 207 */       AtomicTreeConfig atomicTreeConfig = this.m_maker.buildAtomicTreeConfig(attributes);

/* 209 */       this.m_linker.onAtomicTreeConfig(parentObj, atomicTreeConfig);
/* 210 */       this.m_objs.push(atomicTreeConfig);
    } else {
/* 212 */       throw new SAXException(String.format("Element(%s) is not expected under server-filter-config!", new Object[] { qName }));
    }

/* 215 */     this.m_tags.push(qName);
  }

  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 219 */     if ("server-filter-config".equals(qName)) {
/* 220 */       ServerFilterConfig serverFilterConfig = this.m_maker.buildServerFilterConfig(attributes);

/* 222 */       this.m_entity = serverFilterConfig;
/* 223 */       this.m_objs.push(serverFilterConfig);
/* 224 */       this.m_tags.push(qName);
/* 225 */     } else if ("crash-log-domain".equals(qName)) {
/* 226 */       CrashLogDomain crashLogDomain = this.m_maker.buildCrashLogDomain(attributes);

/* 228 */       this.m_entity = crashLogDomain;
/* 229 */       this.m_objs.push(crashLogDomain);
/* 230 */       this.m_tags.push(qName);
/* 231 */     } else if ("atomic-tree-config".equals(qName)) {
/* 232 */       AtomicTreeConfig atomicTreeConfig = this.m_maker.buildAtomicTreeConfig(attributes);

/* 234 */       this.m_entity = atomicTreeConfig;
/* 235 */       this.m_objs.push(atomicTreeConfig);
/* 236 */       this.m_tags.push(qName);
    } else {
/* 238 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }


  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 244 */     if (uri == null || uri.length() == 0) {
/* 245 */       if (this.m_objs.isEmpty()) {
/* 246 */         parseRoot(qName, attributes);
      } else {
/* 248 */         Object parent = this.m_objs.peek();
/* 249 */         String tag = (String)this.m_tags.peek();

/* 251 */         if (parent instanceof ServerFilterConfig) {
/* 252 */           parseForServerFilterConfig((ServerFilterConfig)parent, tag, qName, attributes);
/* 253 */         } else if (parent instanceof CrashLogDomain) {
/* 254 */           parseForCrashLogDomain((CrashLogDomain)parent, tag, qName, attributes);
/* 255 */         } else if (parent instanceof AtomicTreeConfig) {
/* 256 */           parseForAtomicTreeConfig((AtomicTreeConfig)parent, tag, qName, attributes);
        } else {
/* 258 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }

/* 262 */       this.m_text.setLength(0);
    } else {
/* 264 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/server/filter/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */